package ${package.Controller};

import com.github.pagehelper.PageInfo;
import ${package.Vo}.${vo};
import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/${vo?uncap_first}")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>

    @Resource
    ${table.serviceName} ${table.serviceName?uncap_first};

    /**
    * ${table.comment!}首页
    * @return
    */
    @RequestMapping("index")
    public ModelAndView index(HttpServletRequest request) throws Exception {
        ModelAndView mv = new ModelAndView();
        super.getCacheSearchParam(request, mv);
        mv.setViewName("<#if package.ModuleName??>/${package.ModuleName}</#if>/${vo?uncap_first}Index");
        return mv;
    }

    /**
    * ${table.comment!}数据列表
    * @param request
    * @return
    * @throws Exception
    */
    @RequestMapping("list")
    public ModelAndView list(HttpServletRequest request, ${vo} ${vo?uncap_first},
                            @RequestParam(required = false, defaultValue = "1") Integer page,
                            @RequestParam(required = false, defaultValue = "15") Integer rows) throws Exception {
        ModelAndView mv = new ModelAndView();
        super.setCacheSearchParam(request);
        PageInfo<${vo}> ${vo?uncap_first}Page = ${table.serviceName?uncap_first}.select${vo}Page(${vo?uncap_first}, page, rows);
        mv.addObject("${vo?uncap_first}Page", ${vo?uncap_first}Page);
        mv.setViewName("<#if package.ModuleName??>/${package.ModuleName}</#if>/${vo?uncap_first}List");
        return mv;
    }

    /**
    * ${table.comment!}新增页面
    * @return
    * @throws Exception
    */
    @GetMapping("add")
    public ModelAndView add(HttpServletRequest request) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("<#if package.ModuleName??>/${package.ModuleName}</#if>/${vo?uncap_first}Add");
        return mv;
    }

    /**
    * ${table.comment!}编辑页面
    * @return
    * @throws Exception
    */
    @GetMapping("edit")
    public ModelAndView edit(String <#list table.fields as field><#if field.keyFlag>${field.propertyName}</#if></#list>) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("${table.voPath}", ${table.serviceName?uncap_first}.getById(<#list table.fields as field><#if field.keyFlag>${field.propertyName}</#if></#list>));
        mv.setViewName("<#if package.ModuleName??>/${package.ModuleName}</#if>/${vo?uncap_first}Edit");
        return mv;
    }

    /**
    * ${table.comment!}新增保存操作
    * @return
    * @throws Exception
    */
    @RequestMapping("save")
    @ResponseBody
    public String save(HttpServletRequest request, ${vo} ${table.voPath}) throws Exception {
        <#list table.fields as field><#if field.propertyName == 'createAdmin'>${table.voPath}.setCreateAdmin(super.getLoginAdminId(request));</#if><#if field.propertyName == 'createUser'>${table.voPath}.setCreateUser(super.getLoginAdminId(request));</#if></#list>
        boolean success = ${table.serviceName?uncap_first}.save(${table.voPath});
        if (success) {
            return ${table.voPath}.get<#list table.fields as field><#if field.keyFlag>${field.propertyName ? cap_first}</#if></#list>().toString();
        }
        return "";
    }

    /**
    * ${table.comment!}更新保存操作
    * @return
    * @throws Exception
    */
    @RequestMapping("update")
    @ResponseBody
    public String update(HttpServletRequest request, ${vo} ${table.voPath}) throws Exception {
        <#list table.fields as field><#if field.propertyName == 'modifyAdmin'>${table.voPath}.setModifyAdmin(super.getLoginAdminId(request));</#if><#if field.propertyName == 'createUser'>${table.voPath}.setCreateUser(super.getLoginAdminId(request));</#if></#list>
        boolean success = ${table.serviceName?uncap_first}.updateById(${table.voPath});
        if (success) {
            return ${table.voPath}.get<#list table.fields as field><#if field.keyFlag>${field.propertyName ? cap_first}</#if></#list>().toString();
        }
        return "";
    }

    /**
    * ${table.comment!}查看页面
    * @return
    * @throws Exception
    */
    @RequestMapping("view")
    public ModelAndView view(String <#list table.fields as field><#if field.keyFlag>${field.propertyName}</#if></#list>) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("${table.voPath}", ${table.serviceName?uncap_first}.getById(<#list table.fields as field><#if field.keyFlag>${field.propertyName}</#if></#list>));
        mv.addObject("<#list table.fields as field><#if field.keyFlag>${field.propertyName}</#if></#list>", <#list table.fields as field><#if field.keyFlag>${field.propertyName}</#if></#list>);
        mv.setViewName("<#if package.ModuleName??>/${package.ModuleName}</#if>/${vo?uncap_first}View");
        return mv;
    }

    /**
    * 删除${table.comment!}
    * @param request
    * @param <#list table.fields as field><#if field.keyFlag>${field.propertyName}</#if></#list>
    * @return
    * @throws Exception
    */
    @RequestMapping("delete")
    @ResponseBody
    public boolean delete(HttpServletRequest request, String <#list table.fields as field><#if field.keyFlag>${field.propertyName}</#if></#list>) throws Exception {
        return ${table.serviceName?uncap_first}.removeById(<#list table.fields as field><#if field.keyFlag>${field.propertyName}</#if></#list>);
    }
}
</#if>
